package com.ruoyi.assessment.controller;

import com.github.pagehelper.Page;
import com.ruoyi.assessment.entity.AssessmentVo;
import com.ruoyi.assessment.service.IAssessmentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.incident.domain.XlkjIncident;
import com.ruoyi.incident.service.IXlkjIncidentService;
import com.ruoyi.ini.domain.XlkjIni;
import com.ruoyi.ini.service.IXlkjIniService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门绩效考核
 */
@RestController
@RequestMapping("/assessment/dept")
public class AssessmentControllerDept extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
//    @Autowired
//    private IXlkjIncidentService incidentService;
    @Autowired
    private IXlkjIniService iniService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private IAssessmentService assessmentService;

    @GetMapping("/list")
    public TableDataInfo list(){
//        List<AssessmentVo> assessmentVoList = new ArrayList<>();
//
//        //查询所有网格长
//        startPage();

        startPage();
        Page<SysUser> sysUsers = (Page<SysUser>) sysUserService.selectUserListByRoleId(100L);
        Page<AssessmentVo> assessmentVoList=new Page<>();
        assessmentVoList.setTotal(sysUsers.getTotal());
        for(SysUser sysUser:sysUsers){
            AssessmentVo assessmentVo = new AssessmentVo();
            //头像路径拼接
            if(sysUser.getAvatar() != null && sysUser.getAvatar().length() != 0){
                sysUser.setAvatar(UrlUtils.getUrl() + sysUser.getAvatar());
            }
            assessmentVo.setSysUser(sysUser);

            //设置总分值
            assessmentVo.setSource(100);


            /**
             * 计算得分
             *    该部门下事件完成总数 / 该部门下事件上报总数 * 100
             */
            Integer source = 0;

            List<String> list = sysDeptMapper.selectIdByParentId(sysUser.getDeptId());
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            XlkjIni ini = new XlkjIni();
            if(!str.equals("()")){
                ini.setDepartmentList(str);
            }

            List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(ini);
            //设置该部门总上报事件数
            assessmentVo.setIncidentNum(xlkjInis.size());
            ini.setStatus("2");
            List<XlkjIni> xlkjInis1 = iniService.selectXlkjIniList(ini);
            //设置该部门总完成事件数
            assessmentVo.setIncidentSucc(xlkjInis1.size());

            //设置总得分
            Long deptId = sysUser.getDeptId();
            List<SysDept> sysDepts = sysDeptMapper.selectDeptByParentId(deptId);
            String strstr = "(";
            for(int i =0;i<sysDepts.size();i++){
                strstr += sysDepts.get(i).getDeptId();
                if(i != sysDepts.size()-1){
                    strstr += ",";
                }
            }
            strstr += ")";

            if(!strstr.equals("()")){
                List<AssessmentVo> list1 = assessmentService.getList(strstr);
                int sourceAll = 0;
                for(AssessmentVo ass:list1){
                    sourceAll += ass.getSourceNum();
                }

                assessmentVo.setSourceNum(sourceAll / list1.size());
            }else {
                assessmentVo.setSourceNum(0);
            }


            assessmentVoList.add(assessmentVo);
        }
        return getDataTable(assessmentVoList);
    }

}
